package m1_simple;

import com.rabbitmq.client.*;

import java.io.IOException;

public class Consumer {
    public static void main(String[] args) throws Exception{
        //创建连接
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.126.129");
        factory.setUsername("admin");
        factory.setPassword("admin");

        //重用/创建新的通道
        Channel channel = factory.newConnection().createChannel();

        //定义队列
        channel.queueDeclare("helloworld",false,false,false,null);

        //匿名内部类，定义回调对象
        DeliverCallback deliverCallback = new DeliverCallback() {

            @Override
            public void handle(String consumerTag, Delivery message) throws IOException {
                byte[] body = message.getBody();
                String msg = new String(body);
                System.out.println("收到： " + msg);
            }
        };

        //定义取消回调对象
        CancelCallback cancelCallback = new CancelCallback() {

            @Override
            public void handle(String s) throws IOException {

            }
        };


        //从helloworld队列接收消息，消费消息
        channel.basicConsume("helloworld",true,deliverCallback, cancelCallback);
    }
}
